
package org.vectrics.user.client;


import javax.swing.table.AbstractTableModel;
import javax.swing.ImageIcon;
import org.vectrics.user.UserSummary;
import org.vectrics.user.PersonSynch;
import org.apache.log4j.Logger;
import java.text.SimpleDateFormat;
import java.util.Vector;
import java.util.Collection;
//import org.vectrics.common.util.*;



public class UserTableModel extends AbstractTableModel {
    private transient static Logger log = Logger.getLogger(UserTableModel.class);
    private SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yy' 'hh:mm:ss");
	private int rowCount = 0;
	private Vector userHolderList = new Vector();
	    
	public static String COL_ICON = "icon";
    public static String COL_USERNAME = "userName";
    public static String COL_FIRSTNAME = "firstName";
	public static String COL_LASTNAME = "lastName";
    
    private Vector columnCodeList = new Vector();
    private boolean showIndex = false;
	private boolean showLevelStr = true;
    
    public int maximumEventListSize = 2000;
    //public ElsxServerConnection connection = null;
    

    public UserTableModel() {
        super();
        buildColumnList();
        log.debug("MonitorTableModel constructor -");
    }
    
    private void buildColumnList() {
		columnCodeList.clear();
		columnCodeList.add(COL_USERNAME);
		columnCodeList.add(COL_FIRSTNAME);
		columnCodeList.add(COL_LASTNAME);
    }

	public void setMaximumEventListSize(int size) {
		this.maximumEventListSize = size;
	}


	public void setShowIndex(boolean value) {
		this.showIndex = value;
		buildColumnList();
	}
	
	public void setShowLevelStr(boolean value) {
		this.showLevelStr = value;		
		buildColumnList();
	}
	
	
    /*
    public void setElsxConnection(ElsxServerConnection connection) {
        this.connection = connection;
        registerEventsAdded(0, connection.getEventList().size() - 1);
    }
     */

    //public void addLogEvent(LogEvent event) {
    //    msgList.add(event);
    //    fireTableRowsInserted(msgList.size() - 1, msgList.size() - 1);
    //};
    
    //public void registerEventsAdded(int startIndex, int endIndex) {
    //    fireTableRowsInserted(startIndex, endIndex);
    //}


    synchronized public int getRowCount()
    {
        return(userHolderList.size());
    }



    public int getColumnCount() {
        return(columnCodeList.size());
    }


    synchronized public String getColumnName(int columnIndex)  throws java.util.NoSuchElementException {
        String colName = "error";
        
        String columnCode = (String)columnCodeList.elementAt(columnIndex);
        
        if (columnCode.equals(COL_USERNAME)) {
            colName = "User Name";
        }
        else if (columnCode == COL_LASTNAME) {
            colName = "Last Name";
        }
		else if (columnCode == COL_FIRSTNAME) {
			colName = "First Name";
		}
        else {
        	throw new java.util.NoSuchElementException("Out of range, column index = " + columnIndex); 
        }
        return(colName);
    }


    synchronized public Class getColumnClass(int columnIndex) {
		String columnCode = (String)columnCodeList.elementAt(columnIndex);
    	
        if (columnCode == COL_ICON)
            return(ImageIcon.class);
        else
            return(String.class);
    }

    synchronized public boolean isCellEditable(int rowIndex, int columnIndex) {
        return(false);
    }

	public Collection getColumnCodeList() {
		return(columnCodeList);		
	}
	
	public int getColumnIndex(String columnCode) {
		int index = -1;
		int i = 0;
		while ((index < 0) && (i < columnCodeList.size())) {
			String curColumnCode = (String)columnCodeList.elementAt(i);
			if (curColumnCode.compareTo(columnCode) == 0) {
				index = i;
			}
			i++;
		}
		return(index);
	}

    
    synchronized public void setUserHolderList(Collection userHolderList) {
    	if (this.getRowCount() > 0) {
    		this.fireTableRowsDeleted(0, this.getRowCount() - 1);
    	}
    		
		this.userHolderList.clear();
		this.userHolderList.addAll(userHolderList);
		
		if (this.getRowCount() > 0)    	
       		this.fireTableRowsInserted(0, this.getRowCount() - 1);
    }

    
    synchronized public void clear() {
        int size = getRowCount();
        this.userHolderList.clear();
       	this.fireTableRowsDeleted(0, size - 1);
       	rowCount = 0;
    }
    
    
    
    synchronized public PersonSynch getElementAt(int rowIndex) throws java.util.NoSuchElementException
    {
        PersonSynch user = null;
        if (rowIndex < 0) {
            log.error("getEventAt() - requested row is below zero");
        }
        else if (rowIndex >= userHolderList.size()) {
            String message = "getEventAt() - requested row is beyond end of list of events. Request row = " 
            	+ rowIndex + " List Size = " + userHolderList.size();
            log.error(message);
            throw new java.util.NoSuchElementException(message);
        }
        else {
            try {
            	user = (PersonSynch)userHolderList.elementAt(rowIndex);
            }
            catch (Exception ex) {
                log.error("getEventAt() - Exception finding event at row: " 
                	+ rowIndex +  "  List size = " + userHolderList.size(), ex);
            }
        }
        return(user);
    };



    synchronized public Object getValueAt(int rowIndex, int columnIndex) throws java.util.NoSuchElementException {
    	PersonSynch userHolder = getElementAt(rowIndex);
        UserSummary user = userHolder.getUserSummary();
        Object value = null;
		String columnCode = (String)columnCodeList.elementAt(columnIndex);

        if (columnCode == COL_USERNAME) {
            value = user.getUserName();
        }
        else if (columnCode == COL_FIRSTNAME) {
            value = user.getFirstName(); 
        }
        else if (columnCode == COL_LASTNAME) {
			value = user.getLastName();
        }
		else {
			throw new java.util.NoSuchElementException("Column index not found");
		}

        return(value);
    }
}